package org.zappee.dannyt.swiz.control
{
	import mx.collections.IList;
	import mx.events.DynamicEvent;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;
	
	import org.swizframework.controller.AbstractController;
	import org.zappee.dannyt.swiz.business.Bug;
	import org.zappee.dannyt.swiz.model.ZappeeModel;
	import org.zappee.dannyt.swiz.service.IBugService;
	
	public class BugListController extends org.swizframework.controller.AbstractController
	{
		public static const EVENT_GET_BUGS : String = "getBugsEvent";
		public static const EVENT_NEW_BUG : String = "newBugEvent";
		public static const EVENT_EDIT_BUG : String = "editBugEvent";
		
		[Autowire(bean="zappeeModel")]
		public var model:ZappeeModel;
		
		[Autowire(bean="bugService")]
		public var bugService:IBugService;
		
		public function BugListController()
		{
		}
		
		[Mediate(event="getBugsEvent")]
		public function getBugs():void
		{
			var call:AsyncToken = bugService.getBugs();
			executeServiceCall(call, onGetBugs);
		}
		
		private function onGetBugs(re:ResultEvent):void
		{
			this.model.bugs = re.result as IList;
		}
		
		[Mediate(event="newBugEvent")]
		public function newBug():void
		{
			this.model.currentBug = new Bug();
			this.showManageBugView();
		}
		
		[Mediate(event="editBugEvent", properties="bug")]
		public function editBug(bug:Bug):void
		{
			this.model.currentBug = bug;
			this.showManageBugView();
		}
		
		private function showManageBugView():void
		{
			this.model.viewState = ZappeeModel.VIEWING_MANAGE_BUG_SCREEN;
		}
	}
}